package org.custom.spring.boot.security.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.custom.spring.boot.security.interfaces.AuthenToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import static org.cleaver.basic.constant.CleaverConstant.EMPTY;

/**
 * 系统用户数据 DTO
 *
 * @author LiKun
 * @date 2021/11/11 21:51
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SystemUserInfo implements AuthenToken, Serializable {
    private String id;

    private String tokenKey;

    private Collection<SystemRole> authorities;

    /**
     * 获取匿名用户数据
     *
     * @return 匿名用户
     */
    public static SystemUserInfo getAnonymous() {
        return new SystemUserInfo(EMPTY, EMPTY, new ArrayList<SystemRole>() {{
            add(SystemRole.getAnonymous());
        }});
    }

    public SystemUserInfo setTokenKey(String tokenKey) {
        this.tokenKey = tokenKey;
        return this;
    }

    @Override
    public Authentication getAuthentication() {
        return new UsernamePasswordAuthenticationToken(id, tokenKey, authorities);
    }
}
